﻿using Business;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace BVVD_Website.ql_hoc_vien
{
    public partial class frmHocphi2 : System.Web.UI.Page
    {
        public static string mfrm = "MNU023";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                grvHocphi.Culture = new CultureInfo("vi-VN");
                grvHocphi.Rebind();

                BVVDDataContext db = new BVVDDataContext();
                var listLOPHOC = (from lh in db.LST_LOPHOCs
                                  where lh.TRANGTHAI.Equals("1")
                                  select lh).ToList();

                ddlLOPHOC.DataSource = listLOPHOC;
                ddlLOPHOC.DataTextField = "TENLH";
                ddlLOPHOC.DataValueField = "MALH";
                ddlLOPHOC.DataBind();
                // grvGiayChungnhan.DataSource = getData();
                fillgrvHocphi();
                grvHocphi.DataBind();

            }
        }



        protected void grvGiayChungnhan_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();           
            if (Session["user"] != null)
            {
                LST_NSD user = Session["user"] as LST_NSD;

                V_PHANQUYEN pq = (from p in db.V_PHANQUYENs
                                  where p.MAMN.Equals(mfrm) && p.MANSD.Equals(user.MANSD)
                                  select p).SingleOrDefault();
                if (pq != null)
                {
                    grvHocphi.MasterTableView.GetItems(GridItemType.CommandItem)[0].FindControl("lbtnSave").Visible = pq.EDIT > 0 && pq.ADD > 0 && pq.DELETE > 0 ? true : false;
                }
            }






            if(e.Item is GridDataItem){
                GridDataItem item = (GridDataItem)e.Item;
                RadDropDownList ddlLOAITIEN = item.FindControl("ddlLOAITIEN") as RadDropDownList;
                ddlLOAITIEN.SelectedValue = item["MALOAITIEN"].Text;
            }

            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;

                Label lblLOPHOC = editFormItem.FindControl("lblLOPHOC") as Label;

                RadDropDownList ddlMAHV = editFormItem.FindControl("ddlMAHV") as RadDropDownList;
                var listhv = (from hv in db.F_LOP_HOCVIENs
                              where hv.MALH.Equals(ddlLOPHOC.SelectedValue)
                              select new
                              {
                                  MAHV = hv.MAHV,
                                  TENHV = hv.LST_HOCVIEN.HODEM + " " + hv.LST_HOCVIEN.TENHV,
                                  FULLINFO = hv.MAHV + " | " + hv.LST_HOCVIEN.HODEM + " " + hv.LST_HOCVIEN.TENHV
                              }).ToList();
                ddlMAHV.DataSource = listhv;
                ddlMAHV.DataValueField = "MAHV";
                ddlMAHV.DataTextField = "FULLINFO";
                ddlMAHV.DataBind();


                RadDropDownList ddlLOAITIEN1 = editFormItem.FindControl("ddlLOAITIEN1") as RadDropDownList;

                var listcur = (from cur in db.LST_DANHMUCs
                               where cur.LST_LOAIDM.MALOAIDM.Equals("LOAITIEN")
                               && cur.TRANGTHAI.Equals("1")
                               select cur).ToList();
                ddlLOAITIEN1.DataSource = listcur;
                ddlLOAITIEN1.DataValueField = "MADM";
                ddlLOAITIEN1.DataTextField = "TENDM";
                ddlLOAITIEN1.DataBind();
                LinkButton btnUpdate = editFormItem.FindControl("btnUpdate") as LinkButton;




                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {
                    if (ddlLOPHOC.SelectedIndex >= 0)
                    {
                        lblLOPHOC.Text = ddlLOPHOC.SelectedText.ToUpper();
                    }
                    else
                    {
                        lblLOPHOC.Text = "<a href='/ql-hoc-vien/frmHocphi.aspx'>Quay lại để chọn lớp</a>";
                        btnUpdate.Enabled = false;
                    }
                }
                else
                {
                    //Label lblTRANGTHAI = editFormItem["TRANGTHAI"].FindControl("lblTRANGTHAI") as Label;
                    lblLOPHOC.Text = ddlLOPHOC.SelectedText.ToUpper();

                    ddlLOAITIEN1.SelectedValue = editFormItem["MALOAITIEN"].Text;
                    ddlMAHV.SelectedValue = editFormItem["MAHV"].Text;
                    ddlMAHV.Enabled = false;
                }
                //ddlLOAIDM.DataBind();
            }
        }    



        

        protected void ddlLOPHOC_SelectedIndexChanged(object sender, DropDownListEventArgs e)
        {
            //grvGiayChungnhan.DataSource = getData()
            fillgrvHocphi();
            grvHocphi.DataBind();
        }


        private void fillgrvHocphi()
        {

            BVVDDataContext db = new BVVDDataContext();
            var list = (from lhv in db.F_LOP_HOCVIENs
                        where lhv.MALH.Equals(ddlLOPHOC.SelectedValue)
                        join hp in db.F_DONGHOCPHIs on lhv.MAHV equals hp.MAHV into temp1
                        from tmp1 in temp1.DefaultIfEmpty()
                        select new
                        {
                            RECID = tmp1.RECID != null ? tmp1.RECID : 0,
                            MAHV = lhv.MAHV,
                            TENHV = lhv.LST_HOCVIEN.HODEM + " " + lhv.LST_HOCVIEN.TENHV,
                            SOTIEN = tmp1.SOTIEN,
                            LOAITIEN = tmp1.LOAITIEN,
                            NGAYDHP = tmp1.NGAYDHP,
                            LAN = tmp1.LAN,
                            NGUOITHU = tmp1.NGUOITHU,
                            GHICHU = tmp1.GHICHU

                        }).ToList();

            grvHocphi.DataSource = list;


        }

        protected void grvGiayChungnhan_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                //Add new" button clicked
                //Add new" button clicked
                e.Canceled = true;
                //Prepare an IDictionary with the predefined values
                System.Collections.Specialized.ListDictionary newValues = new
                System.Collections.Specialized.ListDictionary();
                newValues["MAHV"] = "";
                newValues["LOAITIEN"] = "";
                newValues["NGAYDHP"] = "";
                newValues["GHICHU"] = "";
                newValues["LAN"] = "";
                newValues["SOTIEN"] = "";
                newValues["NGUOITHU"] = "";
                //Insert the item and rebind
                e.Item.OwnerTableView.InsertItem(newValues);
            }

            if (e.CommandName.Equals("GiayCN"))
            {
                BVVDDataContext db = new BVVDDataContext();
                foreach (GridDataItem dr in grvHocphi.Items)
                {
                    string mahv = dr["MAHV"].Text;
                    string ghichu = dr["GHICHU"].Text;
                    string loaitien = (dr.FindControl("ddlLOAITIEN") as RadDropDownList).SelectedValue;
                    string nguoithu = Session["user"] != null ? (Session["user"] as LST_NSD).MANSD : dr["NGUOITHU"].Text;
                    int lan = Convert.ToInt32((dr.FindControl("txtLAN") as RadNumericTextBox).DbValue);
                    DateTime ngaynhan = Convert.ToDateTime((dr.FindControl("txtNGAYNHAN") as RadDatePicker).IsEmpty ? DateTime.Now : (dr.FindControl("txtNGAYNHAN") as RadDatePicker).DbSelectedDate);
                    decimal sogcn = Convert.ToDecimal((dr.FindControl("txtGCN") as RadNumericTextBox).DbValue);

                    //lblTRANGTHAI.Text = (dr.FindControl("txtGCN") as RadNumericTextBox).DbValue;
                    F_DONGHOCPHI obj = new F_DONGHOCPHI
                    {
                        MAHV = mahv,
                        NGUOITHU = nguoithu,
                        LAN = lan>1?lan:1,
                        LOAITIEN = loaitien,
                        NGAYDHP = ngaynhan,
                        SOTIEN = sogcn,
                        //TRANGTHAI = String.IsNullOrEmpty(trangthai) ? "1" : trangthai,
                        GHICHU = String.IsNullOrEmpty(ghichu) ? "" : ghichu
                    };
                    checkRowExist(obj);


                }

                grvHocphi.Rebind();
                rnAlert.Text = "Lưu trữ thông tin đóng học phí thành công ";
                rnAlert.Show();
            }
        }

        private string checkRowExist(F_DONGHOCPHI fc)
        {
            BVVDDataContext db = new BVVDDataContext();
            F_DONGHOCPHI value = (from ck in db.F_DONGHOCPHIs
                                  where //ck.MALH.Equals(fc.MALH)
                                           ck.MAHV.Equals(fc.MAHV)
                                  select ck).SingleOrDefault();
            if (value == null)
            {
                if (fc.SOTIEN > 0)
                {
                    db.F_DONGHOCPHIs.InsertOnSubmit(fc);
                    db.SubmitChanges();
                    return "Add";
                }
                else
                {
                    return "Dont Save";
                }
            }
            else if (!value.SOTIEN.Equals(fc.SOTIEN) || !value.NGAYDHP.Equals(fc.NGAYDHP) || !value.LAN.Equals(fc.LAN) || !value.LOAITIEN.Equals(fc.LOAITIEN))
            {
                value.SOTIEN = fc.SOTIEN;
                value.NGAYDHP = fc.NGAYDHP;
                value.LOAITIEN = fc.LOAITIEN;
                value.LAN = fc.LAN;
                db.SubmitChanges();
                return "Update";
            }
            else
            {

                return "Cancel";
            }

        }

       

        protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            BVVDDataContext db = new BVVDDataContext();
            var list = (from l in db.LST_DANHMUCs
                        where l.TRANGTHAI.Equals("1") && l.MALOAIDM.Equals("LOAITIEN")
                        select l).ToList();

            e.Result = list;
        }

        protected void grvHocphi_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            fillgrvHocphi();
        }

    }
}